<html>
<head><meta charset="utf-8"><title>how to suggest to deref a `Box` · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20suggest.20to.20deref.20a.20.60Box.60.html">how to suggest to deref a `Box`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232517926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20suggest%20to%20deref%20a%20%60Box%60/near/232517926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20suggest.20to.20deref.20a.20.60Box.60.html#232517926">(Mar 31 2021 at 00:21)</a>:</h4>
<p>I would like to implement a diagnostic suggestion that would suggest dereferencing a <code>Box&lt;T&gt;</code> when something expects a <code>T</code>. E.g.,</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="kt">u32</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">bar</span><span class="p">(</span><span class="n">x</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// should suggest `foo(*x)`</span>
</code></pre></div>
<p>I think I need to add a match arm to <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/check/fn_ctxt/struct.FnCtxt.html#method.check_ref">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/check/fn_ctxt/struct.FnCtxt.html#method.check_ref</a> that matches when</p>
<ul>
<li>the actual type is <code>Adt</code> and the DefId is the same as the box lang item, and</li>
<li><code>self.deref_steps</code> returns <code>Some(x)</code> where <code>x &gt; 0</code>.</li>
</ul>
<p>Is that all, or do I need to add logic to the callers of <code>check_ref</code> to make them call it in more cases in addition?</p>



<a name="232518574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20suggest%20to%20deref%20a%20%60Box%60/near/232518574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20suggest.20to.20deref.20a.20.60Box.60.html#232518574">(Mar 31 2021 at 00:30)</a>:</h4>
<p>Actually for the first step it looks like I can just use <code>AdtDef::is_box()</code></p>



<a name="232521063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20suggest%20to%20deref%20a%20%60Box%60/near/232521063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20suggest.20to.20deref.20a.20.60Box.60.html#232521063">(Mar 31 2021 at 01:04)</a>:</h4>
<p>Hmm, actually it looks like this suggestion already exists for some cases (such as the example I gave). I'm guessing it's not applied in all the cases it could be.</p>



<a name="232521441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20suggest%20to%20deref%20a%20%60Box%60/near/232521441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20suggest.20to.20deref.20a.20.60Box.60.html#232521441">(Mar 31 2021 at 01:10)</a>:</h4>
<p>Ah, here we go:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">enum</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Bar</span><span class="p">(</span><span class="nb">Box</span><span class="o">&lt;</span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">Foo</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">bar</span><span class="p">(</span><span class="n">x</span>: <span class="nc">Foo</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">Foo</span>::<span class="n">Bar</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="n">y</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This code suggests:</p>
<div class="codehilite"><pre><span></span><code>error[E0308]: mismatched types
 --&gt; src/lib.rs:9:28
  |
9 |         Foo::Bar(y) =&gt; foo(y),
  |                            ^
  |                            |
  |                            expected enum `Foo`, found struct `Box`
  |                            help: try using a variant of the expected enum: `Foo::Bar(y)`
  |
  = note: expected enum `Foo`
           found struct `Box&lt;u32&gt;`
</code></pre></div>



<a name="232521468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20suggest%20to%20deref%20a%20%60Box%60/near/232521468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20suggest.20to.20deref.20a.20.60Box.60.html#232521468">(Mar 31 2021 at 01:10)</a>:</h4>
<p>This type of error comes up a lot in my codebase and it's really annoying because all I need to do is add a deref but it suggests re-wrapping the type.</p>
<p>So, it looks like the solution is to also suggest the deref in that case.</p>



<a name="232521919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20suggest%20to%20deref%20a%20%60Box%60/near/232521919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20suggest.20to.20deref.20a.20.60Box.60.html#232521919">(Mar 31 2021 at 01:19)</a>:</h4>
<p>Also, the suggestion is only for Copy types for some reason; I'm guessing that's in case the type is <code>&amp;Box&lt;String&gt;</code> rather than <code>Box&lt;String&gt;</code>, which should work.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>